// pages/impression/place/place.js
const http = require("../../../utils/http.js")
const createRecycleContext = require('miniprogram-recycle-view')
const app = getApp()

var ctx;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    recycleList: [],
    totalCount: 0,
    totalPage: 0,
    searchName: '',
    triggered: false,
    animationData: {},
    floorstatus: false,
    isLoad: true,

    queryParam:{
      pageIndex: 1,
      pageSize: 10,
      lat: '',
      lng: ''
    }
  },

  choose(e){
    let pages = getCurrentPages(); //页面栈
    var prevPage = pages[pages.length - 2]; //上一个页面

    let data = {
      title: e.currentTarget.dataset.title,
      province: e.currentTarget.dataset.province,
      city: e.currentTarget.dataset.city,
      district: e.currentTarget.dataset.district,
      address: e.currentTarget.dataset.address
    }
    prevPage.setData({ //直接给上一个页面赋值
      placeData: data//自己定义的一个名字
    });
    wx.navigateBack({
      delta: 1
    })
  },
  toLowFun(e) {
    let self = this;
    self.setData({
      isLoad: false
    })
    if (self.data.pageIndex >= self.data.totalPage){
      self.setData({
        isLoad: true
      })
      return;
    }
    let param = {
      pageIndex: self.data.queryParam.pageIndex + 1,
      pageSize: 10,
      lat: self.data.queryParam.lat,
      lng: self.data.queryParam.lng,
      keyword: self.data.searchName
    }
    self.setData({
      queryParam: param
    })
    let url = "/impression/getNearby";
    if (self.data.searchName != '') {
      url = "/impression/searchPlace"
    }
    http.get(url, param).then(res => {
      if(res.data.data.length == 0){
        self.setData({
          isLoad: false
        })
      } 
      ctx.append(res.data.data.resultList)
      self.setData({
        totalPage: res.data.data.totalPage,
        totalCount: res.data.data.totalCount
      })
    })
  },

  // 获取滚动条当前位置
  scrolltoupper: function (e) {
    if (e.detail.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  inputChange(e){
    this.setData({
      searchName: e.detail.value
    })
    let self = this;
    self.setData({
      isLoad: false
    })
    let param = {
      pageIndex: 1,
      pageSize: 10,
      lat: self.data.queryParam.lat,
      lng: self.data.queryParam.lng,
      keyword: self.data.searchName
    }
    self.setData({
      queryParam: param
    })
    let url = "/impression/getNearby";
    if (self.data.searchName != '') {
      url = "/impression/searchPlace"
    }
    http.get(url, param).then(res => {
      if (res.data.data.length == 0) {
        self.setData({
          isLoad: false
        })
      }
      ctx.update(0, res.data.data.resultList)
      self.setData({
        totalPage: res.data.data.totalPage,
        totalCount: res.data.data.totalCount
      })
    })
  },
  clearSearchName(){
    this.setData({
      searchName: ''
    })
    this.refresh();
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let self = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        let param = {
          pageIndex: 1,
          pageSize: 10,
          lat: res.latitude,
          lng: res.longitude,
          keyword: self.data.searchName
        }
        self.setData({
          queryParam: param
        })
        let url = "/impression/getNearby";
        if (self.data.searchName != '') {
          url = "/impression/searchPlace"
        }
        http.get(url, param).then(res => {
          ctx = createRecycleContext({
            id: 'recycleId',
            dataKey: 'recycleList',
            page: self,
            itemSize: {
              width: 100,
              height: 200
            },
            useInPage: false
          })
          ctx.append(res.data.data.resultList)

          self.setData({
            totalPage: res.data.data.totalPage,
            totalCount: res.data.data.totalCount
          })
        })
      }
    })
  },

  //回到顶部
  goTop: function (e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },
  //刷新
  refresh: function (e) {
    let self = this;
    self.setData({
      topNum: self.data.topNum = 0
    });
    let id = setInterval(() => {
      app.rotateAni(self, 'animationData', 'num')
    }, 500)
    let param = {
      pageIndex: 1,
      pageSize: 10,
      lat: self.data.queryParam.lat,
      lng: self.data.queryParam.lng,
      keyword: self.data.searchName
    }
    self.setData({
      queryParam: param
    })
    let url = "/impression/getNearby";
    if(self.data.searchName != ''){
      url = "/impression/searchPlace"
    }
    http.get(url, param).then(res => {
      ctx.update(0, res.data.data.resultList)
      self.setData({
        totalPage: res.data.data.totalPage,
        totalCount: res.data.data.totalCount
      })
      clearInterval(id);
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})